package com.zzty.taskapp.servlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zzty.taskapp.dao.UserDAO;
import com.zzty.taskapp.daoImpl.UserDAOImpl;
import com.zzty.taskapp.entity.User;
import com.zzty.taskapp.service.SelfExamService;
import com.zzty.taskapp.service.SelfExamTransationService;
import com.zzty.taskapp.service.TeamExamService;
import com.zzty.taskapp.service.TeamExamTransationService;
import com.zzty.taskapp.serviceImpl.SelfExamServiceImpl;
import com.zzty.taskapp.serviceImpl.SelfExamTransationServiceImpl;
import com.zzty.taskapp.serviceImpl.TeamExamServiceImpl;
import com.zzty.taskapp.serviceImpl.TeamExamTransationServiceImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@WebServlet(name="SearchScoreServlet",urlPatterns="/SearchScoreServlet")
public class SearchScoreServlet extends HttpServlet {
	protected TeamExamService tes = new TeamExamServiceImpl();
	protected TeamExamTransationService tets = new TeamExamTransationServiceImpl();
	protected SelfExamTransationService sets = new SelfExamTransationServiceImpl();
	protected SelfExamService ses = new SelfExamServiceImpl();
	protected UserDAO ud = new UserDAOImpl();
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		User user = (User) req.getSession().getAttribute("user");
		int classid = Integer.valueOf(user.getClasses());
		PrintWriter out = resp.getWriter();
		String allDate = readStringFromReq(req);
		String dates[] = allDate.split("_");
		JSONArray jsonArray = new JSONArray();
		JSONArray jsonArray1 = new JSONArray();
		JSONArray jsonArray2 = new JSONArray();
		System.out.println("-------"+dates.length);
		for(int i = 0;i<dates.length;i++){
			String date = dates[i];
			int examId = ses.searchIdByDateAndClass(date, classid);
			if(examId != 0){
				JSONObject json = new JSONObject();
				Map<String,String> stuIdandScoreSelf = sets.searchIdAndScore(examId);
				String selfnames = "";
				String selfscores = "";
				for (Object obj : stuIdandScoreSelf.keySet()) {
			            String key = (String) obj;
			            String name = "";
			            try {
							User stu = ud.findAllByID(Integer.valueOf(key));
							if(stu.getType() == 1){
								continue;
							}
							name = stu.getDisname();
						} catch (Exception e) {
							e.printStackTrace();
						}
			            String value = (String) stuIdandScoreSelf.get(key);
			            if(value == null){
			            	value = " ";
			            }
			            System.out.print(key + "=" + value+"\t");
			            selfnames += (name+"_");
			            selfscores += (value+"_");
			        }
				json.put("date", date);
				json.put("names", selfnames);
				json.put("scores", selfscores);
				jsonArray1.add(json);
			}
		}
		for(int i = 0;i<dates.length;i++){
			String date = dates[i];
			int examId = tes.searchExamId(date, classid);
			if(examId != 0){
				JSONObject json = new JSONObject();
				Map<String,String> stuIdandScoreTeam = tets.searchScoreByExamid(examId);
				String teamnames = "";
				String teamscores = "";
				for (Object obj : stuIdandScoreTeam.keySet()) {
		            String key = (String) obj;
		            String value = (String) stuIdandScoreTeam.get(key);
		            if(value == null){
		            	value = " ";
		            }
		            String name = "";
		            String score = "";
		            if(key.length()>3){
		            	String tmps[] = key.split("\\|");
		            	try {
		            		User stu1 = ud.findAllByID(Integer.valueOf(tmps[0]));
		            		User stu2 = ud.findAllByID(Integer.valueOf(tmps[1]));
		            		name = stu1.getDisname()+"_"+stu2.getDisname();
		            		score = value+"_"+value;
		            	} catch (Exception e) {
		            		e.printStackTrace();
		            	}
		            }else{
		            	try {
							User stu = ud.findAllByID(Integer.valueOf(key));
							name = stu.getDisname();
							score = value;
						} catch (Exception e) {
							e.printStackTrace();
						}
		            }
		            System.out.print(name + "=" + score+"\t");
		            teamnames += (name+"_");
		            teamscores += (score+"_");
		        }
				json.put("date", date);
				json.put("names", teamnames);
				json.put("scores", teamscores);
				jsonArray2.add(json);
			}
		}
		jsonArray.add(jsonArray1);
		jsonArray.add(jsonArray2);
		System.out.println(jsonArray1.toString());
		System.out.println(jsonArray2.toString());
		System.out.println(jsonArray.toString());
		out.print(jsonArray.toString());
	}
	
	
	protected String readStringFromReq(HttpServletRequest req) {
		StringBuffer sb = new StringBuffer();
		String line = null;
		try {
			BufferedReader br = req.getReader();
			while ((line = br.readLine()) != null) {
				sb.append(line);

			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
}
